package com.ayun.member.service.impl;

import com.ayun.base.Result;
import com.ayun.member.entity.Member;
import com.ayun.member.mapper.MemberMapper;
import com.ayun.member.req.MemberREQ;
import com.ayun.member.service.IMemberService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * @author 斌叔
 * @description: 继承 ServiceImpl<MemberMapper, Member> 类 , 并且实现 IMemberService 接口
 * @create 2022-05-08 23:18
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IMemberService {


    @Override
    public Result search(long page, long size, MemberREQ req) {
        QueryWrapper<Member> query = new QueryWrapper<>();
        if (req != null) {
            if (req.getName() != null && !req.getName().isEmpty()) {
                query.like("name", req.getName());
            }
            if (StringUtils.isNotBlank(req.getCardNum())) {
                query.like("card_num", req.getCardNum());
            }
            if (StringUtils.isNotBlank(req.getPayType())) {
                query.eq("pay_type", req.getPayType());
            }
            if (req.getBirthday() != null) {
                query.eq("birthday", req.getBirthday());
            }

        }


        // 封装分页对象
        IPage<Member> p = new Page<>(page, size);
        IPage<Member> data = baseMapper.selectPage(p, query);
        return Result.ok(data);
    }

    @Override
    public Result update(int id, Member member) {
        if (member.getId() == null) {
            member.setId(id);
        }
        //更新操作
        int size = baseMapper.updateById(member);
        if (size < 1) {
            return Result.error("修改会员信息失败");
        }
        return Result.ok();
    }
}
